<?php

namespace Console\Logic;

use \Common\Logic\BaseLogic;

/**
 * 推荐收徒logic
 */
class RecommLogic extends BaseLogic
{
    private $confModel;
    private $userModel;
    private $joinModel;

    public function __construct()
    {
        parent::__construct();
        $this->confModel = D('Recommconf');
        $this->userModel = D('User');
        $this->joinModel = D('Join');
    }


    /**
     * 获取分成配置
     * @param array $where
     * @return mixed
     */
    public function getConfInfo($where = array())
    {
        $info = $this->confModel->where($where)->find();
        return static::$res->suc("成功", $info);
    }


    /**
     * 编辑分成配置
     * @param $id
     * @param $userId
     * @param array $param
     * @return mixed
     */
    public function editConf($id, $userId, $param = array())
    {
        if (empty($id)) {
            return static::$res->err("id不能为空");
        }

        if (empty($userId)) {
            return static::$res->err("编辑人不能为空");
        }

        $where = array(
            "id" => $id
        );

        $data = array();
        if (!empty($param['upper'])) {
            $data['upper'] = $param['upper'] * 100;
        }
        if (!empty($param['per'])) {
            if ($param['per'] > 50) {
                return static::$res->err("抽成比不能大于50%");
            }
            $data['per'] = $param['per'];
        }

        $data['edit_time'] = date('Y-m-d H:i:s');
        $data['edit_id'] = $userId;

        $count = $this->confModel->where($where)->save($data);
        if (empty($count)) {
            return static::$res->err("数据库异常，没有任何改变");
        }

        return static::$res->suc("操作成功");
    }

    /**
     * 6度循环获取徒弟裂变裂变
     *
     * @param [type] $parentId
     * @return void
     */
    public function getRecommUserList($parentId)
    {
        if (empty($parentId)) {
            return static::$res->err("推手ID不能为空");
        }

        $userList = array();
        $ids = $parentId;

        //最多递归层级
        $maxLevel = 6;
        for ($i=0; $i < $maxLevel; $i++) {
            $where = array(
                "parent_id"=>array("in",$ids),
                "user_state"=>1
            );
            $itemUserList = $this->userModel->where($where)->select();

            if (empty($itemUserList)) {
                $i = $maxLevel;
                continue;
            }

            $ids = array();
            foreach ($itemUserList as $vo) {
                $ids[] = $vo["id"];
                $userList[] = $vo;
            }
        }

        return static::$res->suc("成功", $userList);
    }

    /**
     * 获取裂变徒弟全部审核通过的任务
     *
     * @param [type] $parentId
     * @param [type] $finishTime
     * @param integer $page
     * @param integer $size
     * @return void
     */
    public function getRecommTaskList($parentId, $finishTime, $page = 1, $size = 50)
    {
        if (empty($parentId) || empty($finishTime)) {
            return static::$res->err("参数异常");
        }

        $userListRes = $this->getRecommUserList($parentId);
        $userList = $userListRes['Result'];
        if (empty($userList)) {
            return static::$res->err("推手没有任何徒弟");
        }

        $ids = array();
        $userDic = array();
        foreach ($userList as $vo) {
            $voUserId = $vo['id'];
            $ids[] = $voUserId;
            $userDic[$voUserId] = $vo;
        }

        $where = array(
            "user_id"=>array("in",$ids),
            "state"=>3,
            "finish_time"=>array(
                array('EGT',$finishTime."-01 00:00:00"),
                array('ELT',$finishTime."-31 23:59:59"),
                'and'
            )
        );

        $statistics = $this->joinModel->field("count(*) as count,sum(income) as incomes")->where($where)->select();
        $list = $this->joinModel->where($where)->order("id desc")->page($page, $size)->select();

        foreach ($list as &$vo) {
            $vo["user_name"] = $userDic[$vo['user_id']]['name'];
            $vo["user_photo"] = $userDic[$vo['user_id']]['photo'];
        }

        $data = array(
            "list"=>$list,
            "incomes"=>$statistics[0]['incomes'],
            "count"=>$statistics[0]['count']
        );

        return static::$res->suc("成功", $data);
    }
}
